package com.example.device.vm

import com.example.common.view.vm.BaseViewModel
import com.example.device.api.DeviceApiService
import com.example.device.entity.DeviceEntityItem
import com.example.device.entity.DeviceOrderAddParam
import com.example.device.entity.DeviceOrderEntityItem
import com.example.device.entity.DeviceParam
import com.example.device.model.DeviceRepository
import com.example.device.model.DeviceResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DeviceOrderViewModel @Inject constructor(private val apiService: DeviceApiService):BaseViewModel<DeviceRepository,DeviceResultData<List<DeviceOrderEntityItem>>>(){
    fun getListDataOrder(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getDeviceOrder()
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data=result.data
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getListDataOrderXiang(id:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getDeviceOrderXiang(id)
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data= listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getListDataStartInspection(id:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.startInspection(id)
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data= listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getListDataTransferInspection(id:Int,inspector:String){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.transferInspection(id,inspector)
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data= listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getListDataDeviceOrderAdd(deviceOrderAddParam:DeviceOrderAddParam){
        mViewModelScope.launch (mDispatcher){
            try {
                val result=apiService.getDeviceOrderAdd(deviceOrderAddParam)
                val res=DeviceResultData(
                    state=result.state,
                    msg=result.msg,
                    data= listOf(result.data)
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}